/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.DailyDetailEntity;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class DailyDetailSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    private EntityManager em;
    private DailyDetailEntity dd;
    private SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");

    public DailyDetailEntity getDailyDetailByDate(Date d){
        Timestamp ts = new Timestamp(d.getTime());
        System.out.println(ts);
        java.sql.Date sd = new java.sql.Date(d.getTime());
        Query query = em.createQuery("SELECT u FROM DailyDetailEntity u where u.ts = :dd");
        query.setParameter("dd", ts);
        System.out.println("--------------------Check Point--------------------");
        System.out.println(query.getResultList().size());
        DailyDetailEntity dd = null;
        try {
            dd = (DailyDetailEntity) query.getSingleResult();
        } catch (NoResultException ex) {
            System.out.println("No result");
            //throw ex;
        }
        //System.out.println(dd.getDate());
        return dd;
    }

    public DailyDetailEntity getDailyDetailById(Long id) {
        return em.find(DailyDetailEntity.class, id);
    }

    public void addDailyDetail(DailyDetailEntity dd) {

        dd.getTs().setTime(dd.getDate().getTime());
        em.persist(dd);

    }

    public void updateDailyDetail(DailyDetailEntity dd) {

        dd.getTs().setTime(dd.getDate().getTime());
        em.merge(dd);

    }

    public void generateAYear(int year) throws ParseException {
        Date d = null;
        Calendar cal = new GregorianCalendar();
        String s = "01/01/" + year;
        try {
            d = formatter.parse(s); 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(d);
        Date temp = null;
        while ((cal.getTime().getYear()+1900) == year) {
            if (cal.get(Calendar.DAY_OF_WEEK) ==  1 || cal.get(Calendar.DAY_OF_WEEK) == 7) {
                cal.add(Calendar.DATE, 1);
                System.out.println(cal.getTime());
            } else {
                DailyDetailEntity dde = new DailyDetailEntity();
                dde.setDate(cal.getTime());
                System.out.println("--------------Date Check---------------");
                System.out.println(dde.getDate());
                dde.getTs().setTime(dde.getDate().getTime());
                System.out.println("--------------Timestamp Check---------------");
                System.out.println(dde.getTs());
                System.out.println("--------------STRDate Check---------------");
                addDailyDetail(dde);
                cal.add(Calendar.DATE, 1);
            }
        }
        return;
    }

    public void removeDailyDetail(Date d) {
        DailyDetailEntity dd = getDailyDetailByDate(d);
        em.remove(dd);
    }

    public DailyDetailEntity getDd() {
        return dd;
    }

    public void setDd(DailyDetailEntity dd) {
        this.dd = dd;
    }

    public List<DailyDetailEntity> getPeriod(Date d1, Date d2) {
        List<DailyDetailEntity> period = new ArrayList<DailyDetailEntity>();
        Calendar cal = new GregorianCalendar();
        cal.setTime(d1);
        while (cal.getTime().before(d2)) {
            period.add(getDailyDetailByDate(cal.getTime()));
            cal.add(Calendar.DATE, 1);
        }
        period.add(getDailyDetailByDate(d2));
        return period;
    }
}
